import { defineStore } from "pinia";
import { ElMessage } from "element-plus";
import axios from "axios";
import dayjs from "dayjs";

const useStore = defineStore({
  id: "CEMStore",
  state: () => ({
    areas: [],
    sectors: [],
    params: {},
    realCompute: {
      areaResult: [],
      policyResult: [],
      sectorResult: [],
      computeTime: 0,
      dataSize: 0,
    },
    currentView: {
      selectedMode: "auto",
      timeStep: 1,
    },
  }),
  getters: {},
  actions: {
    async loadData() {
      await this.getAreas();
      await this.getSectors();
    },
    getAreas() {
      return new Promise((resolve, reject) => {
        axios
          .get("api/cem_area_get.php")
          .then((response) => {
            if (response.data.result) {
              this.areas = response.data.result;
              this.areas.sort();
              resolve(response.data.result);
            } else {
              this.areas = [];
              console.warn("cem_area_get", response.data);
              reject(response.data);
            }
          })
          .catch((error) => {
            console.error("cem_area_get", error);
            reject(error);
          });
      });
    },
    getSectors() {
      return new Promise((resolve, reject) => {
        axios
          .get("api/cem_sector_get.php")
          .then((response) => {
            if (response.data.result) {
              this.sectors = response.data.result;
              this.sectors.sort();
              resolve(response.data.result);
            } else {
              this.sectors = [];
              console.warn("cem_sector_get", response.data);
              reject(response.data);
            }
          })
          .catch((error) => {
            console.error("cem_sector_get", error);
            reject(error);
          });
      });
    },
    saveArea(formData) {
      return new Promise((resolve, reject) => {
        axios
          .post("api/cem_area_set.php", formData)
          .then((response) => {
            console.log("cem_area_set", response.data);
            if (response.data.success) {
              this.getAreas();
              ElMessage({
                type: "success",
                message: `空域组合"${formData.identifier}"已保存`,
              });
              resolve();
            } else {
              ElMessage({
                type: "warning",
                message: "空域组合保存失败",
              });
              reject();
            }
          })
          .catch((error) => {
            console.error("cem_area_set", error);
            ElMessage({
              type: "error",
              message: "空域组合保存失败",
            });
            reject();
          });
      });
    },
    saveSector(formData) {
      return new Promise((resolve, reject) => {
        axios
          .post("api/cem_sector_set.php", formData)
          .then((response) => {
            console.log("cem_sector_set", response.data);
            if (response.data.success) {
              this.getSectors()
                .then(() => {
                  ElMessage({
                    type: "success",
                    message: `扇区方案"${formData.title}"已保存`,
                  });
                  resolve();
                })
                .catch(() => {
                  ElMessage({
                    type: "warning",
                    message: `扇区方案"${formData.title}"已保存，但列表刷新失败`,
                  });
                  reject();
                });
            } else {
              ElMessage({
                type: "warning",
                message: "扇区方案保存失败",
              });
              reject();
            }
          })
          .catch((error) => {
            console.error("cem_sector_set", error);
            ElMessage({
              type: "error",
              message: "扇区方案保存失败",
            });
            reject();
          });
      });
    },
    deleteArea(formData) {
      return new Promise((resolve, reject) => {
        axios
          .post("api/cem_area_del.php", { identifier: formData.identifier })
          .then((response) => {
            if (response.data.result) {
              this.getAreas();
              ElMessage({
                type: "success",
                message: "空域组合 " + formData.identifier + " 已删除",
              });
              resolve(true);
            } else {
              console.warn("cem_area_del", response.data);
              ElMessage({
                type: "warning",
                message: "空域组合删除失败",
              });
              reject(false);
            }
          })
          .catch((error) => {
            console.error("cem_area_del", error);
            ElMessage({
              type: "error",
              message: "空域组合删除失败",
            });
            reject(false);
          });
      });
    },
    deleteSector(item) {
      return new Promise((resolve, reject) => {
        axios
          .post("api/cem_sector_del.php", { number: item.number })
          .then((response) => {
            if (response.data.result) {
              this.getSectors()
                .then(() => {
                  ElMessage({
                    type: "success",
                    message:
                      "空域组合 [" + item.number + "]" + item.title + " 已删除",
                  });
                  resolve(true);
                })
                .catch(() => {
                  ElMessage({
                    type: "warning",
                    message:
                      "空域组合 [" +
                      item.number +
                      "]" +
                      item.title +
                      " 已删除，但列表刷新失败",
                  });
                  reject(false);
                });
            } else {
              console.warn("cem_sector_del", response.data);
              ElMessage({
                type: "warning",
                message: "空域组合删除失败",
              });
              reject(false);
            }
          })
          .catch((error) => {
            console.error("cem_sector_del", error);
            ElMessage({
              type: "error",
              message: "空域组合删除失败",
            });
            reject(false);
          });
      });
    },
  },
});

export default useStore;
